/**
  ******************************************************************************
  * @file    stm32f0xx_it.c 
  * @author  MCD Application Team
  * @version V1.0.0
  * @date    17-January-2014
  * @brief   Main Interrupt Service Routines.
  *          This file provides template for all exceptions handler and 
  *          peripherals interrupt service routine.
  ******************************************************************************
  * @attention
  *
  * <h2><center>&copy; COPYRIGHT 2014 STMicroelectronics</center></h2>
  *
  * Licensed under MCD-ST Liberty SW License Agreement V2, (the "License");
  * You may not use this file except in compliance with the License.
  * You may obtain a copy of the License at:
  *
  *        http://www.st.com/software_license_agreement_liberty_v2
  *
  * Unless required by applicable law or agreed to in writing, software 
  * distributed under the License is distributed on an "AS IS" BASIS, 
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
  *
  ******************************************************************************
  */ 

/* Includes ------------------------------------------------------------------*/
#include "stm32f0xx_it.h"
#include "pwm.h"



/** @addtogroup STM32F0XX_IT
  * @brief Interrupts driver modules
  * @{
  */

/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
/* Private function prototypes -----------------------------------------------*/
/* Private functions ---------------------------------------------------------*/

__IO uint16_t IC2Value = 0;
__IO uint16_t uwDutyCycle = 0;
__IO uint32_t uwFrequency = 0;

__IO uint16_t IC15Value = 0;
__IO uint16_t uw15DutyCycle = 0;
__IO uint32_t uw15Frequency = 0;

/**
  * @brief  This function handles NMI exception.
  * @param  None
  * @retval None
  */
void NMI_Handler(void)
{
}

/**
  * @brief  This function handles Hard Fault exception.
  * @param  None
  * @retval None
  */
void HardFault_Handler(void)
{
  /* Go to infinite loop when Hard Fault exception occurs */
  while (1)
  {
  }
}

/**
  * @brief  This function handles SVCall exception.
  * @param  None
  * @retval None
  */
void SVC_Handler(void)
{
}

/**
  * @brief  This function handles PendSVC exception.
  * @param  None
  * @retval None
  */
void PendSV_Handler(void)
{
}

/**
  * @brief  This function handles SysTick Handler.
  * @param  None
  * @retval None
  */
void SysTick_Handler(void)
{

}
/**
  * @brief  This function handles TIM2 global interrupt request.
  * @param  None
  * @retval None
  */
extern void LEDXToggle(uint8_t ledx);
int Tick1MS = 0;
void TIM1_BRK_UP_TRG_COM_IRQHandler(void)
{
  TIM_ClearITPendingBit(TIM1, TIM_IT_Update);
	Tick1MS = 1;
}

void TIM3_IRQHandler()
{
	/* Clear TIM2 Capture compare interrupt pending bit */
  TIM_ClearITPendingBit(TIM3, TIM_IT_CC2);

  /* Get the Input Capture value */
  IC2Value = TIM_GetCapture2(TIM3);

  if (IC2Value != 0)
  {
    /* Duty cycle computation */
    uwDutyCycle = (TIM_GetCapture1(TIM3) * 1000) / IC2Value;

    /* Frequency computation */
    uwFrequency = SystemCoreClock / IC2Value;
  }
  else
  {
    uwDutyCycle = 0;
    uwFrequency = 0;
  }
}

void TIM15_IRQHandler()
{
	/* Clear TIM2 Capture compare interrupt pending bit */
  TIM_ClearITPendingBit(TIM15, TIM_IT_CC2);

  /* Get the Input Capture value */
  IC15Value = TIM_GetCapture2(TIM15);

  if (IC15Value != 0)
  {
    /* Duty cycle computation */
    uw15DutyCycle = (TIM_GetCapture1(TIM15) * 1000) / IC15Value;

    /* Frequency computation */
    uw15Frequency = SystemCoreClock / IC15Value;
  }
  else
  {
    uw15DutyCycle = 0;
    uw15Frequency = 0;
  }
}


#define RXBUFFERSIZE 64
uint8_t RxBuffer[RXBUFFERSIZE];
volatile uint16_t RxCount = 0; 
volatile uint16_t ReadCount = 0; 
volatile int TXE_Flag = 0;
volatile int RXNE_Flag = 0;
int isfull(void)
{
	if(ReadCount < RxCount + 1 )
		return 0;
	else return 1;
}

void clearUartData()
{
	RxCount = 0;
	ReadCount = 0; 
}

uint8_t readUartData()
{
	uint8_t read = RxBuffer[ReadCount];
	int tail  = RxCount;
	if( ReadCount > tail )
	{
		 tail = tail + RXBUFFERSIZE;
	}
	if( ReadCount < tail )
	{
		 ReadCount++;
	}
	if( ReadCount >= RXBUFFERSIZE )
		ReadCount = 0;
	return read;
}

int isReadBufferEmpty()
{
	if( ReadCount == RxCount )
	{
		  return 1;
	}
	else return 0;
}



void USART1_IRQHandler(void)
{
	uint8_t rcv;
	
  if(USART_GetITStatus(USART1, USART_IT_RXNE) != RESET)
  {
    /* Read one byte from the receive data register */
    rcv = (USART_ReceiveData(USART1) & 0xFF);
		if((ReadCount != (RxCount + 1)%RXBUFFERSIZE))
		{
			RxBuffer[RxCount] = rcv;
			USART_SendData(USART2,rcv);
			RxCount++;
			if(RxCount>=RXBUFFERSIZE)
					RxCount = 0;
		}
		RXNE_Flag = 1;
  }else if(USART_GetFlagStatus(USART1,USART_FLAG_ORE) != RESET)                                             	 //
  {                                                                                                    //
		USART_ReceiveData(USART1);
		USART_ClearFlag(USART1,USART_FLAG_ORE);                                                                    	 //
  }else if (USART_GetFlagStatus(USART1, USART_FLAG_FE) != RESET) 
	{
		USART_ReceiveData(USART1); 
		USART_ClearFlag(USART1, USART_FLAG_FE);
	} else if(USART_GetFlagStatus(USART1, USART_FLAG_PE) != RESET)         
	{        
		USART_ReceiveData(USART1);  
		USART_ClearFlag(USART1, USART_FLAG_PE);  			  
	}	

  if(USART_GetITStatus(USART1, USART_IT_TXE) != RESET)
  {   
//    /* Write one byte to the transmit data register */
//    USART_SendData(USART1, TxBuffer[TxCount++]);
      USART_ITConfig(USART1, USART_IT_TXE, DISABLE);
		  TXE_Flag = 1;
    }
//  }
}

/******************************************************************************/
/*                 STM32F0xx Peripherals Interrupt Handlers                   */
/*  Add here the Interrupt Handler for the used peripheral(s) (PPP), for the  */
/*  available peripheral interrupt handler's name please refer to the startup */
/*  file (startup_stm32f0x8.s).                                               */
/******************************************************************************/

/**
  * @brief  This function handles PPP interrupt request.
  * @param  None
  * @retval None
  */
/*void PPP_IRQHandler(void)
{
}*/

/**
  * @}
  */

/**
  * @}
  */

/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
